﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Traceability.Commont.Domains;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.Client
{
    public class ClientUpdateHandler : IRequestHandler<ClientUpdateCommand, Resqults>
    {
        private readonly IBateRepository<Clients> clientBate;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="clientBate"></param>
        /// <param name="mapper"></param>
        public ClientUpdateHandler(IBateRepository<Clients> clientBate, IMapper mapper)
        {
            this.clientBate = clientBate;
            this.mapper = mapper;
        }
        /// <summary>
        /// 修改客户管理表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Resqults> Handle(ClientUpdateCommand request, CancellationToken cancellationToken)
        {
            var resqults = new Resqults() { Code = 0 };
            try
            {
                var res = await clientBate.GetAll().Where(p => p.ClientID == request.ClientID).FirstAsync();
                if (res != null)
                {
                    res.ClientType = request.Clients.ClientType;
                    res.Phone = request.Clients.Phone;
                    res.ClientName = request.Clients.ClientName;
                    res.ClientCard = request.Clients.ClientCard;
                    res.AddressName = request.Clients.AddressName;
                    res.ClientAddress = request.Clients.ClientAddress;
                    res.ClientTrade = request.Clients.ClientTrade;
                    res.DateTeamwork = request.Clients.DateTeamwork;
                    res.ClientStart = request.Clients.ClientStart;
                    res.Remark = request.Clients.Remark;
                    res.DelStart = false;
                    var str = await clientBate.Update(res);
                    if (str > 0)
                    {
                        resqults.Date = str;
                        resqults.Messges = "修改成功";
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return resqults;
        }
    }
}
